package com.net.tool;

import java.lang.reflect.Constructor;
import java.util.HashMap;

import com.net.tool.BasicDownload.DownloadTable;

import android.content.Context;
import android.database.Cursor;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;

class DownLoadRunable implements Runnable {

	BasicDownload mCurrentTask;
	Context mContext;

	static HashMap<String, BasicDownload> mRunningTask;

	static boolean mIsAlive = true;

	static {
		mRunningTask = new HashMap<String, BasicDownload>();
	}

	public DownLoadRunable(Context context) {
		mContext = context;
	}

	protected void setCurrentTask(BasicDownload download) {

		synchronized (DownLoadRunable.class) {

			if (download != null) {
				mRunningTask.put(download.mDownloadTaskId, download);
			}
			if (mCurrentTask != null) {
				mRunningTask.remove(mCurrentTask.mDownloadTaskId);
			}
			mCurrentTask = download;
		}
	}

	public static HashMap<String, BasicDownload> getAllRunningList() {

		synchronized (DownLoadRunable.class) {
			return mRunningTask;
		}

	}

	protected BasicDownload getCurrentTask() {

		synchronized (DownLoadRunable.class) {
			return mCurrentTask;
		}

	}

	protected BasicDownload getNextExcuteTask() {

		long pro = System.currentTimeMillis();

		synchronized (BasicDownload.class) {

			BasicDownload mNextbasicDownload = null;

			Cursor cursor = BasicDownload.getSqlite(mContext).query(
					DownloadTable.TB_NAME,
					new String[] { DownloadTable.TASKID, DownloadTable.TASKURL,
							DownloadTable.PRIORITY, DownloadTable.SERVER_VC,
							DownloadTable.TASKSTATE, DownloadTable.CLASS },
					DownloadTable.TASKSTATE + "=? or "
							+ DownloadTable.TASKSTATE + "=? ",
					new String[] { BasicDownload.DOWNLOAD_STATE_PREPARE + "",
							BasicDownload.DOWNLOAD_STATE_PENDING + "" }, null,
					null, DownloadTable.PRIORITY + " desc");

			if (cursor.moveToFirst()) {

				try {
					Log.d("class", cursor.getString(5));
					Class<?> classType = Class.forName(cursor.getString(5));
					Constructor<?> constructor = classType
							.getDeclaredConstructor(new Class[] { Context.class });
					mNextbasicDownload = (BasicDownload) constructor
							.newInstance(mContext);
					Log.d("download", "反射实例化成功");
					mNextbasicDownload.mDownloadTaskId = cursor.getString(0);
					mNextbasicDownload.mUrl = cursor.getString(1);
					mNextbasicDownload.mPriority = cursor.getInt(2);
					mNextbasicDownload.mServerVc = cursor.getInt(3);
					mNextbasicDownload
							.updataTaskState(BasicDownload.DOWNLOAD_STATTE_DOWNLOADING);
					Log.d("download", mNextbasicDownload.mDownloadTaskId
							+ " state= " + cursor.getString(4));

				} catch (Exception e) {
					e.printStackTrace();

					Log.d("download", "反射调用异常==" + e.getMessage());

				}

			} else {
				mNextbasicDownload = null;
				BasicDownload.updataTaskState(-1, "", mContext);

			}

			cursor.close();
			Log.d("getNextExcuteTask", " 耗时="
					+ (System.currentTimeMillis() - pro));
			return mNextbasicDownload;
		}

	}

	@SuppressWarnings("deprecation")
	public void run() {

		while (mIsAlive) {

			setCurrentTask(getNextExcuteTask());

			BasicDownload task = getCurrentTask();

			if (task == null) {
				Log.d("download", "没有下载任务了 请自挂东南枝");
				return;
			}
			Log.d("download", mCurrentTask.mDownloadTaskId + "执行下载");
			PowerManager pm = (PowerManager) mContext
					.getSystemService(Context.POWER_SERVICE);
			int mode = PowerManager.FULL_WAKE_LOCK
					| PowerManager.ACQUIRE_CAUSES_WAKEUP;
			mode = mode | PowerManager.ON_AFTER_RELEASE;
			WakeLock wl = pm.newWakeLock(mode, "SoundRecorder");
			wl.acquire();
			mCurrentTask.downloading();
			if (wl.isHeld()) {
				wl.release();
			}

			Log.d("download", mCurrentTask.mDownloadTaskId + "执行下载结束");

			Log.d("download", "获取下一次下载任务");

		}
	}
}
